找传奇、传世资源到传世资源站!

Wpf 自定义滚动条样式模板

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

参考网上资料,进行修改。定义了有上下按钮和无上下按钮2种滚动条样式模板,
每种滚动条定义7或8副图像,
半透明和无按钮的滚动条需Scrollbackground(x).png图像,上下按钮为全透明图像即可。
添加样式:
1、替换(x)为数字,例如(3),添加相应的图片到资源
2、调整滚动块高度和上下位置距离:
<Track.Thumb>
<Thumb Style="{StaticResource ThumbStyle(2)}" 
    Height="79"
    Margin="-1,0,-1,0" />
</Track.Thumb>

设置上下按钮高度
<Grid x:Name="Bg"
    Background="{TemplateBinding Background}"
    SnapsToDevicePixels="true">
    <Grid.RowDefinitions>
       <RowDefinition MaxHeight="39" />
       <RowDefinition Height="0.00001*" />
       <RowDefinition MaxHeight="39" />
    </Grid.RowDefinitions>

如无上下按钮,则设置
<ImageBrush x:Key="ScrollBackground(x)" 
 TileMode="FlipY" 
ImageSource="/Resource_ScrollImage/Scrollbackground(1).png" />
......
<ControlTemplate TargetType="{x:Type ScrollBar}">
<Grid x:Name="Bg"
Background="{DynamicResource ScrollBackground(1)}"
SnapsToDevicePixels="true">
......

背景透明
<ScrollBar x:Name="PART_VerticalScrollBar"
 Background="Transparent"from clipboard
from clipboard public partial class MainWindow : Window { bool change; public string fontFace = Environment.CurrentDirectory @"\中文字体.ttf#萝莉体 第二版"; Grid scrollGrid; ScrollViewer scrollViewer; TextBlock textBlock; public MainWindow() { InitializeComponent(); ImageBrush background = new ImageBrush(); background.ImageSource = new BitmapImage(new Uri(Environment.CurrentDirectory @"\background.jpg")); background.Stretch = Stretch.Fill; this.Background = background; scrollGrid = (Grid)XamlToObj (@" <Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' Name='scrollGrid' HorizontalAlignment='Left' VerticalAlignment='Top' Margin='5' Height='360' Width='320'/>" ); scrollViewer = (ScrollViewer)XamlToObj(@" <ScrollViewer xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' Name='scrollViewer' HorizontalAlignment='Stretch' VerticalAlignment='Stretch' Margin='0' Focusable='false'> </ScrollViewer> "); textBlock = (TextBlock)XamlToObj(@" <TextBlock xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' Name='textBlock' HorizontalAlignment='Stretch' VerticalAlignment='Stretch' FontFamily='" fontFace @"' FontSize='20' Foreground='White' Margin ='0' TextWrapping = 'Wrap' Text='" Res_Content.conter_1.Replace("\n", "&#x0a;&#x0d;").Replace("\r", "&#x0a;&#x0d;") @"'> <TextBlock.Effect> <DropShadowEffect Color='Black' BlurRadius='5' Opacity='0.8'></DropShadowEffect> </TextBlock.Effect > </TextBlock> "); scrollViewer.Template = Resources["ScrollViewerControlTemplate(1)"] as ControlTemplate; scrollViewer.Content = textBlock; scrollGrid.Children.Add(scrollViewer); GridMain.Children.Add(scrollGrid); change = true; } public object XamlToObj(string xaml) { string strXaml = xaml; StringReader strreader = new StringReader(strXaml); XmlTextReader xmlreader = new XmlTextReader(strreader); object obj = XamlReader.Load(xmlreader); return obj; } private void btn_change_Click(object sender, RoutedEventArgs e) { change = !change; if (change) scrollViewer.Template = Resources["ScrollViewerControlTemplate(1)"] as ControlTemplate; else scrollViewer.Template = Resources["ScrollViewerControlTemplate(2)"] as ControlTemplate; } }

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复